package com.yikaiye.android.yikaiye.ui.activity.mine.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.http.ApiFactory;
import com.yikaiye.android.yikaiye.model.UserModel;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UserInfoViewModel extends BaseViewModel {

    public MutableLiveData<UserModel> userModelMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> urlMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> uploadErrorMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> changeSuccessMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> changeErrorMutableLiveData = new MutableLiveData<>();

    /**
     * 个人信息
     */
    public void getUserInfo() {
        addSubscribe(mRetrofitHelper.getUserInfo(MyApplication.manager.getUserDetailId()).subscribeWith(new BaseNetCallback<UserModel>() {
            @Override
            public void onSuccess(BaseModel<UserModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    userModelMutableLiveData.setValue(baseModel.getData());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }

    /**
     * 头像上传
     *
     * @param compressFile 压缩后的图片
     */
    public void uploadHeadImg(File compressFile) {
        MultipartBody.Part part = null;
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), compressFile);
        try {
            // 路径有中文名要进行编码
            part = MultipartBody.Part.createFormData("file", URLEncoder.encode(compressFile.getName(), "utf-8"), requestBody);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        addSubscribe(mRetrofitHelper.uploadImage(part).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    urlMutableLiveData.setValue(ApiFactory.IMAGE_UPLOAD + baseModel.getData());
                } else {
                    uploadErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                uploadErrorMutableLiveData.setValue(message);
            }
        }));
    }

    /**
     * 修改个人信息
     */
    public void updateUserInfo(Map<String, Object> map) {
        addSubscribe(mRetrofitHelper.updateUserInfo(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    changeSuccessMutableLiveData.setValue("success");
                } else {
                    changeErrorMutableLiveData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                changeErrorMutableLiveData.setValue(message);
            }
        }));
    }
}
